from flask_jwt_extended import JWTManager
from flask_jwt_extended import jwt_required, get_jwt_identity, verify_jwt_in_request, get_jwt
from exception.api_exception import APIException
from functools import wraps

jwt = JWTManager()  # jwt 生成token


def all_jwt_required():
    def wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            try:
                verify_jwt_in_request()
                claims = get_jwt()
            except Exception:
                raise APIException("鉴权失败", code=403)
            if claims:
                return fn(*args, **kwargs)
            else:
                raise APIException("鉴权失败", code=403)

        return decorator

    return wrapper
